package com.seal.tsms.base.initializer;

import com.seal.tsms.base.config.ApplicationConfig;
import com.seal.tsms.base.config.WebConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate5.support.OpenSessionInViewFilter;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.Filter;

/**
 * 项目启动入口
 * 使用web3.0 中 可以使用ServletContainerInitializer来实现零xml配置Servlet ,Filter,Listener
 * spring 中提供了AbstractAnnotationConfigDispatcherServletInitializer来简化springmvc 和spring 上下文的启动编写
 *
 * Created by seal.jing on 2015/12/20.
 */
public class BaseBootInitializer extends AbstractAnnotationConfigDispatcherServletInitializer implements WebApplicationInitializer {
    protected Logger log= LoggerFactory.getLogger(getClass());
    {
        logger.info(getClass().getName()+" 的对象初始化 .....");
    }
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[]{ApplicationConfig.class};
    }

    @Override
    protected String getServletName() {
        return "spring_mvc";
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[]{WebConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"*.action","*.json","*.do"};
    }

    @Override
    protected Filter[] getServletFilters() {
        OpenSessionInViewFilter osiv=new OpenSessionInViewFilter();
        CharacterEncodingFilter ce=new CharacterEncodingFilter();
        ce.setEncoding("utf-8");
        ce.setForceEncoding(true);
        return new Filter[]{osiv,ce};
    }
}
